package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Organization;
import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.query.ProductQueryObject;
import cn.wolfcode.crm.service.IOrganizationService;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    IProductService productService;
    @Autowired
    IOrganizationService organizationService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"product:view","信息面"},logical = Logical.OR)
    public String view() {
        return "product";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Product> selectAll() {
        return productService.selectAll();
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"product:query","机构列表"},logical = Logical.OR)
    public PageResult query(ProductQueryObject qo) {
        return productService.query(qo);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"product:saveOrUpdate","机构新增/更改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Product dept) {
        Organization organization = organizationService.selectId(dept.getOrganizationId().getId());
        try {
            if (dept.getId() != null) {
                productService.updateByPrimaryKey(dept,organization);
            } else {
                //保存新的员工要设置默认的状态为在职
                productService.insert(dept,organization);
            }
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark(e.getMessage());
        }
    }
    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"productService:changeState","解除合作关系"},logical = Logical.OR)
    public JsonResult changeState(Long id) {
        try {

            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("操作失败");
        }
    }

    @RequestMapping("selectProductByOrganizationId")
    @ResponseBody
    @RequiresPermissions(value = {"productService:changeState","解除合作关系"},logical = Logical.OR)
    public List<Product> selectProductByOrganizationId(Long id) {
        return productService.selectProductByOrganizationId(id);
    }
}
